package com.baolai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baolai.entity.GoodsDevice;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.GoodsDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "备件的用途设备")
@ResponseBody
@RestController
@RequestMapping("/goods-device")
public class GoodsDeviceController {

    @Autowired
    GoodsDeviceService goodsDeviceService;

    @ApiOperation(value = "分页查询设备")
    @PostMapping("listDevice")
    public Result listDevice(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(goodsDeviceService.listDevice(jsonObject.getString("key"),jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

    @ApiOperation(value = "添加用途设备")
    @PostMapping("add")
    public Result addGoodsDevice(@RequestBody GoodsDevice goodsDevice){
        if(goodsDeviceService.addGoodsDevice(goodsDevice)){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @ApiOperation(value = "删除用途设备")
    @PostMapping("delete")
    public Result deleteGoodsDevice(@RequestBody JSONObject jsonObject){
        if(goodsDeviceService.deleteGoodsDevice(jsonObject.getString("goodsId"),jsonObject.getString("deviceId"))){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }

    @ApiOperation(value = "根据备件编码查询其用途设备")
    @PostMapping("listByGoodsId")
    public Result listGoodsDeviceByGoodsId(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(goodsDeviceService.listGoodsDeviceByGoodsId(jsonObject.getString("goodsId"),jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

}
